/* $Id: NewsActivity.java 83 2007-12-19 06:48:54Z osborn.steven $
 * 
 * Undroid Package Manager
 *
 * Copyright 2007 Steven Osborn
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.bitsetters.undroid;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.ListActivity;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageView;

import com.bitsetters.android.common.BulletedText;
import com.bitsetters.android.common.BulletedTextListAdapter;
import com.bitsetters.undroid.common.CommonMenu;
import com.bitsetters.undroid.common.Footer;

/**
 * 
 * @author Steven Osborn - http://steven.bitsetters.com
 */
public class NewsActivity extends ListActivity {

    public static final String TAG = "UNDROID[News]";
    public static final int NEWS_LOAD_ERROR = 100;
    public static boolean newsFetched = false;
    public static final String NEWS_FEED = "http://steven.bitsetters.com/feed";

    /**
     * 
     */
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.news);
        ImageView logo = (ImageView) findViewById(R.id.logo_icon);
        logo.setImageDrawable(getResources()
                .getDrawable(R.drawable.installer32));
        logo.setPadding(3, 3, 7, 3); //l,t,r,b

        ImageView android_logo = (ImageView) findViewById(R.id.android_logo);
        android_logo.setImageDrawable(getResources()
                .getDrawable(R.drawable.undroid2));
        android_logo.setPadding(0,10,0,3);

        List<BulletedText> newsItems = null;
        try {
            newsItems = getNewsList(this);
            newsFetched = true;
        } catch(Exception e) {
            Log.e(TAG,e.toString());

            // Could not load categories xml, notify user
            NotificationManager nm = (NotificationManager)
            getSystemService(NOTIFICATION_SERVICE);
            nm.notifyWithText(NEWS_LOAD_ERROR,
                    getText(R.string.news_load_error),
                    NotificationManager.LENGTH_SHORT, null);
        }

        BulletedTextListAdapter btla = new BulletedTextListAdapter(this);
        btla.setListItems(newsItems);
        setListAdapter(btla);
        
        Footer.setup(this);
    }

    /**
     * Parses news from a wordpress RSS feed 
     *  
     * @param xpp
     * @return
     * @throws XmlPullParserException
     * @throws IOException
     */
    public static List<BulletedText> getNewsList(Context context) 
    throws XmlPullParserException, IOException {

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser xpp = factory.newPullParser();
            
        URL u = new URL(NewsActivity.NEWS_FEED);
        InputStream in = u.openStream();
        xpp.setInput(in, "ISO-8859-1");
        List<BulletedText> newsItems = new ArrayList<BulletedText>();
        int eventType = xpp.getEventType();
        String currentText = "";
        boolean storiesStarted = false;

        Drawable newsIcon = context.getResources()
            .getDrawable(R.drawable.news16);
        
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if(eventType == XmlPullParser.START_TAG) {
                if(xpp.getName().compareTo("item") == 0 ) {
                    storiesStarted = true;
                }
            } else if(eventType == XmlPullParser.END_TAG) {
                if(xpp.getName().compareTo("title") == 0 && storiesStarted) {
                    BulletedText item;
                    item = new BulletedText(currentText.trim(),newsIcon);
                    newsItems.add(item);
                    item = null;
                }
                currentText = "";
            } else if(eventType == XmlPullParser.TEXT) {
                currentText += xpp.getText();
            }
            eventType = xpp.next();
        }
        return newsItems;
    }
    
    /**
     * 
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        return CommonMenu.createMenu(this, menu);
    }

    /**
     * 
     */
    @Override
    public boolean onOptionsItemSelected(Menu.Item item) {
        return CommonMenu.handleOption(item);
    }
}
